<template>
	<view class="">
		<page-meta>
			<navigation-bar :title="nbTitle" :subtitle-color="nbFrontColor" :loading="nbLoading"
				:front-color="nbFrontColor" :background-color="nbBackgroundColor" />
		</page-meta>
		<my-list :list="lastPost" :loadStatus="loadStatus2" @reflash="reflash"></my-list>
		<add-post-tag @click="click" @reflash="reflash"></add-post-tag>
		<!-- 加载状态 -->
	</view>
</template>

<script>
	import {
		getMyTopic

	} from '@/api/topic/topic.js'
	export default {

		data() {
			return {
				nbTitle: '家校圈',
				nbLoading: false,
				nbFrontColor: '#000000',
				nbBackgroundColor: '#fcc10d',
				loadStatus2: 'loadmore',
				loadStatus1: null,
				shareCover: '',
				lastPost: [],
				total: null,
				queryParams: {
					pageNum: 1, //第几页
					pageSize: 10, //显示多少条
				},
				show: true,
				addclick: true,
			};
		},
		onShow() {
			if (!this.show) {
				let newQueryParams = {
					pageNum: 1, //第几页
					pageSize: this.queryParams.pageNum * this.queryParams.pageSize, //显示多少条
				}
				let list = []
				getMyTopic(this.newQueryParams).then((res) => {
					if (!this.addclick) {
						if (this.lastPost[0].topicId != res.rows[0].topicId) {
							this.lastPost.unshift(res.rows[0])
						}
						this.addclick = true
					}

					for (let i = 0; i < res.rows.length; i++) {
						for (let j = 0; j < this.lastPost.length; j++) {
							if (this.lastPost[j].topicId == res.rows[i].topicId) {
								this.lastPost[j].topicCollection = res.rows[i].topicCollection
								this.lastPost[j].topicContent = res.rows[i].topicContent
								this.lastPost[j].topicLikes = res.rows[i].topicLikes
								this.lastPost[j].topicCount = res.rows[i].topicCount
								this.lastPost[j].readCount = res.rows[i].readCount
								this.lastPost[j].topicImgList = res.rows[i].topicImgList
								this.lastPost[j].topicVideo = res.rows[i].topicVideo
								this.lastPost[j].topicStatus = res.rows[i].topicStatus
							}
						}
					}
				})
				this.show = true
			}

		},
		onLoad() {
			this.getLastPost();

		},
		onReachBottom() {
			this.queryParams.pageNum++;
			this.getLastPost();

		},
		onPullDownRefresh() {
			this.pageNum = 1;
			this.lastPost = [];
			this.getLastPost();
			uni.stopPullDownRefresh();
		},
		methods: {
			click() {
				this.addclick = false
			},
			reflash() {
				// console.log(1);
				this.show = false;
			},
			//获取最新帖子
			getLastPost() {
				this.loadStatus2 = 'loading';
				if (this.loadStatus1 === null) {
					getMyTopic(this.queryParams).then((res) => {
						// console.log(res);
						if (res.code == 200) {
							this.lastPost = this.lastPost.concat(res.rows);
							this.total = res.total
							if (this.queryParams.pageNum * this.queryParams.pageSize >= this.total) {
								setTimeout(() => {
									this.loadStatus2 = 'nomore';
									this.loadStatus1 = "loadmore"
								}, 300)

							} else {
								this.loadStatus2 = 'loadmore';
							}
						} else {
							setTimeout(() => {
								this.loadStatus2 = 'nomore';
							}, 300)
						}
					})
				} else {
					setTimeout(() => {
						this.loadStatus2 = 'nomore';
					}, 300)
				}

			},
		}
	};
</script>

<style>
</style>
